<template>
	<view>
		<view class="">
			<img v-if="login_img.topimg" :src="url + login_img.topimg" alt="" class="" style="width: 100%;height:360rpx;" />
		</view>
		<view class="" style="font-size: 40rpx;text-align: center;margin-top: 20rpx;">
			-请选择不同身份入口登录-
		</view>
		<!-- 城市服务方 -->

		<view class="item" @click="cs_center" style="margin-top: 50rpx;width: 87%;"
			v-if="Object.keys(userinfo).length > 0">
			<img v-if="login_img.city_img" :src="url + login_img.city_img" alt="" class="imgs" />
		</view>
		<button v-if="pass_login == 0 && Object.keys(userinfo).length <= 0" class="item " open-type="getPhoneNumber"
			@getphonenumber="cs_getphonenumber" style="border: none;background: #fff;width: 88%;">

			<img v-if="login_img.city_img" :src="url + login_img.city_img" alt="" class="imgs" />

		</button>
		<button v-if="pass_login == 1 && Object.keys(userinfo).length <= 0" class="item " @click="pass"
			style="border: none;background: #fff;width: 88%;">

			<img  v-if="login_img.city_img" :src="url + login_img.city_img" alt="" class="imgs" />

		</button>

		<!-- 医生登录 -->
		<view class="item" @click="ys_center" v-if="Object.keys(clinic_userinfo).length > 0">
			<img v-if="login_img.clinic_img" :src="url + login_img.clinic_img" alt="" class="imgs" />
		</view>
		<button v-else class="item " open-type="getPhoneNumber" @getphonenumber="ys_getphonenumber"
			style="border: none;background: #fff;width: 88%;">

			<img v-if="login_img.clinic_img" :src="url + login_img.clinic_img" alt="" class="imgs" />

		</button>

		<!-- 患者登录 -->
		<view class="item" @click="tocenter" v-if="Object.keys(hz_userinfo).length > 0">
			<img v-if="login_img.user_img" :src="url + login_img.user_img" alt="" class="imgs" />
		</view>
		<!-- <view class="item" @click="hz_getphonenumber" v-else>
			
		</view> -->
		<button v-else class="item " open-type="getPhoneNumber" @getphonenumber="hz_getphonenumber"
			style="border: none;background: #fff;width: 88%;">

			<img v-if="login_img.user_img" :src="url + login_img.user_img" alt="" class="imgs" />

		</button>
		<view class="db"
			style="position: fixed;bottom: 0px;left:0px;right:0px;width:95%;display: flex;align-items: center;justify-content: space-around;padding:20rpx;background-color: #fff;border-top: 1px solid #ccc;padding-bottom: 30rpx;">
			<view class="" @click="swwd()" style="display: flex;flex-direction: column;align-items: center;">
				<view class="">
					<image src="../../static/index/zhishiku.png" style="width: 60rpx;height: 60rpx;" mode=""></image>
				</view>
				<view class="" style="font-size: 20rpx;">
					乐友康健
				</view>
			</view>
			<!-- <view class="" @click="tz">
				<view class="">
					<image src="../../static/index/zhenliao.png" style="width: 60rpx;height: 60rpx;" mode=""></image>
				</view>
				<view class="" style="font-size: 20rpx;">
					商城
				</view>
			</view> -->
			<view class="">
				<view class="">
					<image src="../../static/index/fuwu1.png" style="width: 60rpx;height: 60rpx;" mode=""></image>
				</view>
				<view class="" style="font-size: 20rpx;color:#1262DB">
					服务方
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				imglist: [
					'wechatimg/ysd.png',
					'wechatimg/hhr.png',
					'wechatimg/hzd.png',
					'wechatimg/logintop.png',
				],
				hz_userinfo: {}, // 用户
				clinic_userinfo: {}, // 医生
				userinfo: {}, // 合伙人
				pass_login: 0,
				login_img:{
					topimg:'',
					city_img:'',
					clinic_img:'',
					user_img:'',
				}
			}
		},
		onShow() {
			this.hz_userinfo = uni.getStorageSync('hz_userinfo')
			this.clinic_userinfo = uni.getStorageSync('clinic_userinfo')
			this.userinfo = uni.getStorageSync('userinfo')
			
		},
		onLoad() {
			var that = this
			// 先获取是否开启了账号密码登录
			uni.request({
				url: that.global.url + 'api/index/index',
				method: 'POST',
				data: {

				},
				success(res) {
					let result = res.data
					console.log(result)
					if (result.code == 1) {
						that.pass_login = result.data.pass_login
						console.log('设置成功')
						console.log(that.login_img)
					} else {
						uni.showToast({
							title: result.msg,
							icon: 'none'
						})
					}
				},
				complete() {
					uni.hideLoading()
				}
			})
			that.getbg()
		},
		methods: {
			getbg(){
				var that = this
				// 获取登录背景图
				uni.request({
					url: that.global.url + 'api/index/loginbg',
					method: 'POST',
					data: {
				
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							that.login_img = result.data
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			tz() {
				uni.navigateToMiniProgram({
					appId: 'wx83aebf162ebea54d', // 小程序的appid
					path: 'zofui_sales/pages/index/index', // 打开的页面路径，可选
					extraData: {
						// 需要传递给小程序的数据，可选
					},
					success(res) {
						// 打开成功的回调，可选
						console.log('打开成功');
					},
					fail(err) {
						// 打开失败的回调，可选
						console.log('打开失败', err);
					}
				});
			},
			swwd() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			// 携带账号密码登录
			pass() {
				console.log('这里吗')
				uni.navigateTo({
					url: '/pages/login/passlogin'
				})
			},
			ys_center() {
				uni.navigateTo({
					url: '/pages/device/clinic'
				})
			},
			// 去患者个人中心
			tocenter() {

				// uni.setStorageSync('token',uni.getStorageSync('lyk_token'))
				// uni.setStorageSync('userinfo',uni.getStorageSync('lyk_userinfo'))
				// uni.setStorageSync('system_type',4)
				uni.navigateTo({
					url: '/pages/hz_my/hz_my'
				});

			},

			cs_center() {
				uni.navigateTo({
					url: '/pages/personalCenter/personalCenter'
				});
			},
			// 患者端登录，获取手机号码
			hz_getphonenumber(e) {
				console.log(e)
				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.login({
					success: function(loginRes) {
						uni.request({
							url: that.global.url + 'api/user/hz_login',
							method: 'POST',
							data: {
								code: e.detail.code,
								openidcode: loginRes.code
							},
							success(res) {
								let result = res.data
								console.log(result)
								// return false;
								if (result.code == 1) {
									// 存用户信息
									uni.setStorageSync('hz_userinfo', result.data.userinfo)
									// 存用户信息
									uni.setStorageSync('hz_token', result.data.userinfo.token)
									that.tocenter()
								} else {
									uni.showToast({
										title: result.msg,
										icon: 'none'
									})
								}
							},
							complete() {
								uni.hideLoading()
							}
						})
					}
				});
			},

			hz_login(mobile) {
				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: this.global.url + 'api/user/hz_login',
					method: 'POST',
					data: {
						mobile: mobile
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							// 存用户信息
							uni.setStorageSync('hz_userinfo', result.data.userinfo)
							// 存用户信息
							uni.setStorageSync('hz_token', result.data.userinfo.token)
							that.tocenter()
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},

			us_login(mobile) {

				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: this.global.url + 'api/user/devicelogin',
					method: 'POST',
					data: {
						mobile: mobile
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							// 存用户信息
							uni.setStorageSync('clinic_userinfo', result.data)
							// 存用户信息
							uni.setStorageSync('clinic_id', result.data.id)
							console.log('登录成功')
							uni.navigateTo({
								url: '/pages/device/clinic'
							})
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},



			ys_getphonenumber(e) {
				console.log(e)
				var that = this
				uni.showLoading({
					title: '加载中...'
				})

				uni.login({
					success: function(loginRes) {
						console.log(loginRes);
						uni.request({
							url: that.global.url + 'api/user/get_phone',
							method: 'POST',
							data: {
								code: e.detail.code,
								openidcode: loginRes.code
							},
							success(res) {
								let result = res.data
								console.log(result)
								if (result.code == 1) {
									// that.ys_login(13831589771)

									that.ys_login(result.data)
									
									
								} else {
									uni.showToast({
										title: result.msg,
										icon: 'none'
									})
								}
							},
							complete() {
								uni.hideLoading()
							}
						})
					}
				});



				// this.login(18143079335)
			},
			ys_login(mobile) {
				console.log('登录的二手机' + mobile)
				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: this.global.url + 'api/user/devicelogin',
					method: 'POST',
					data: {
						mobile: mobile
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							// 存用户信息
							uni.setStorageSync('clinic_userinfo', result.data)
							// 存用户信息
							uni.setStorageSync('clinic_id', result.data.id)
							console.log('登录成功')
							that.ys_center()
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},

			// 城市服务放登录
			cs_getphonenumber(e) {
				console.log(e)
				var that = this
				uni.showLoading({
					title: '加载中...'
				})



				uni.login({
					success: function(loginRes) {
						console.log(loginRes);
						uni.request({
							url: that.global.url + 'api/user/get_phone',
							method: 'POST',
							data: {
								code: e.detail.code,
								openidcode: loginRes.code
							},
							success(res) {
								let result = res.data
								console.log(result)
								if (result.code == 1) {
									// that.login(13561338081)
									that.login(result.data)
								} else {
									uni.showToast({
										title: result.msg,
										icon: 'none'
									})
								}
							},
							complete() {
								uni.hideLoading()
							}
						})
					}
				});



				// this.login(18143079335)
			},
			login(mobile) {

				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: this.global.url + 'api/user/mobilelogin',
					method: 'POST',
					data: {
						mobile: mobile
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							// 存用户信息
							uni.setStorageSync('userinfo', result.data.userinfo)
							// 存用户信息
							uni.setStorageSync('token', result.data.userinfo.token)
							that.cs_center()
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},


		}
	}
</script>

<style>
	.item {
		width: 88%;
		height: 190rpx;

		margin: 0px auto;
		margin-top: 70rpx;
	}

	.imgs {
		height: 100%;
		width: 100%;
	}

	button {
		margin: 0;
		padding: 0;
		border: none;
		background: none;
		font-family: inherit;
		font-size: inherit;
		cursor: pointer;
		outline: none;
	}
</style>